﻿using TjZheng.Editor;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TjZheng.Framework;
using TjZheng.Domain.Business;
using TjZheng.Domain.DataEntity;

namespace TjZheng.CMS
{
    /// <summary>
    /// UEHandle 的摘要说明
    /// </summary>
    public class UEHandle : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            Handler action = null;
            var Request = context.Request;
            if (Request["action"] == "config")
            {
                action = new ConfigHandler(context);
                action.Process();
                return;
            }

            long fileId = 0;
            long imageId = 0;
            List<UploadLimit> objUploadLimitList = null;
            BizUploadLimit objBizUploadLimit = new BizUploadLimit();
            if (long.TryParse(Request["ImageId"], out imageId) && long.TryParse(Request["FileId"], out fileId))
            {
                objUploadLimitList = objBizUploadLimit.GetListIn(string.Join(",", imageId, fileId));
            }
            else
            {
                if (Request["ImageCategoryCode"].IsNoNull() && Request["ImageLimitCode"].IsNoNull() && Request["FileCategoryCode"].IsNoNull() && Request["FileLimitCode"].IsNoNull())
                {
                    objUploadLimitList = objBizUploadLimit.GetList(string.Format("(UcCode='{0}' AND LimitCode='{1}') OR ((UcCode='{2}' AND LimitCode='{3}')", Request["ImageCategoryCode"], Request["ImageLimitCode"]), Request["FileCategoryCode"], Request["FileLimitCode"]);
                }
            }
            if (objUploadLimitList.IsNull())
            {
                action = new NotSupportedHandler(context) { ErrorMessage = "未设置上传配置" };
                action.Process();
                return;
            }
            UploadLimit objImageLimit = objUploadLimitList.FirstOrDefault(c => c.MediaType == 0);
            UploadLimit objFileLimit = objUploadLimitList.FirstOrDefault(c => c.MediaType == 1);
            switch (Request["action"])
            {
                #region 图片上传
                case "uploadimage":
                    if (objImageLimit == null)
                    {
                        action = new NotSupportedHandler(context) { ErrorMessage = "未设置上传图片配置" };
                        action.Process();
                        return;
                    }
                    action = new UploadHandler(context, new UploadConfig(objImageLimit)
                    {
                        UploadFieldName = Config.GetString("imageFieldName")
                    });
                    break;
                case "catchimage":
                    if (objImageLimit == null)
                    {
                        action = new NotSupportedHandler(context) { ErrorMessage = "未设置上传图片配置" };
                        action.Process();
                        return;
                    }
                    action = new CrawlerHandler(context, objImageLimit);
                    break;
                case "uploadscrawl":
                    if (objImageLimit == null)
                    {
                        action = new NotSupportedHandler(context) { ErrorMessage = "未设置上传图片配置" };
                        action.Process();
                        return;
                    }
                    action = new ScrawlHandle(context, new UploadConfig(objImageLimit)
                    {
                        UploadFieldName = Config.GetString("scrawlFieldName"),
                        Base64Filename = "scrawl.png"
                    });
                    break;
                case "listimage":
                    action = new ListFileManager(context, Config.GetString("imageManagerListPath"), Config.GetStringList("imageManagerAllowFiles"));
                    break;
                #endregion

                #region 文件上传
                case "uploadvideo":
                    if (objFileLimit == null)
                    {
                        action = new NotSupportedHandler(context) { ErrorMessage = "未设置上传文件配置" };
                        action.Process();
                        return;
                    }
                    action = new UploadHandler(context, new UploadConfig(objFileLimit)
                    {
                        //AllowExtensions = Config.GetStringList("videoAllowFiles"),
                        //PathFormat = Config.GetString("videoPathFormat"),
                        //SizeLimit = Config.GetInt("videoMaxSize"),
                        UploadFieldName = Config.GetString("videoFieldName")
                    });
                    break;
                case "uploadfile":
                    if (objFileLimit == null)
                    {
                        action = new NotSupportedHandler(context) { ErrorMessage = "未设置上传文件配置" };
                        action.Process();
                        return;
                    }
                    action = new UploadHandler(context, new UploadConfig(objFileLimit)
                    {
                        //AllowExtensions = Config.GetStringList("fileAllowFiles"),
                        //PathFormat = Config.GetString("filePathFormat"),
                        //SizeLimit = Config.GetInt("fileMaxSize"),
                        UploadFieldName = Config.GetString("fileFieldName")
                    });
                    break;
                case "listfile":
                    action = new ListFileManager(context, Config.GetString("fileManagerListPath"), Config.GetStringList("fileManagerAllowFiles"));
                    break;
                #endregion

                default:
                    action = new NotSupportedHandler(context) { ErrorMessage = "action 参数为空或者 action 不被支持。" };
                    break;
            }
            action.Process();
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}